<?php
namespace Dynamic\Model;
use Think\Model;
class DynamicModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_dynamic'; 
	
	public function getDynamicList($data){
		if(!empty($data['custid'])){
			$map['custid']  = $data['custid'];
		}
		
		if(!empty($data['custids']) && is_array($data['custids'])){
			$map['custid']  = array('in' , $data['custids']);
		}
		
		if(!empty($data['visible']) && is_array($data['visible'])){
			$map['visible']  = array('in' , $data['visible']);
		}
		
		
		$total = $this->where($map)->count();
		$fields = array('dynamicid','custid','content','files','video','video_cover','visible','createtime','updatetime');
		
		if(empty($data['sortField']))$data['sortField'] = "dynamicid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else 
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		return array('total'=>$total , 'data'=>$result);
	}
	
	public function getDynamicById($dynamicid){
		$map['dynamicid'] = $dynamicid;
		$fields = array('dynamicid','custid','content','files','video','video_cover','visible','createtime','updatetime');
		$result = $this->field($fields)->where($map)->find();
		return $result;
	}
	
	public function getDynamicByCust($custid){
		$map['custid'] = $custid;
		$fields = array('dynamicid','custid','content','files','video','video_cover','visible','createtime','updatetime');
		$result = $this->field($fields)->where($map)->order("dynamicid desc")->limit(0 , 20)->select();
		return $result;
	}
	
	public function addDynamic($data){
		$result = $this->add($data);
		return $result;
	}
	
	public function updateDynamic($data){
		$map['dynamicid'] = $data['dynamicid'];
		$map['custid'] = $data['custid'];
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	public function deleteDynamic($data){
		$map['dynamicid'] = $data['dynamicid'];
		$map['custid'] = $data['custid'];
		$result = $this->where($map)->delete();
		return $result;
	}
	
}


